package com.boluo.customer.service.impl;

import java.util.List;

import com.boluo.common.constant.BusinessConstants;
import com.boluo.common.utils.DateUtils;
import com.boluo.common.utils.PhoneUtils;
import com.boluo.common.utils.SecurityUtils;
import com.boluo.customer.dto.CustomerLinkmanDTO;
import org.springframework.stereotype.Service;
import com.boluo.customer.mapper.CustomerLinkmanMapper;
import com.boluo.customer.domain.CustomerLinkman;
import com.boluo.customer.service.ICustomerLinkmanService;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

/**
 * 客户联系人Service业务层处理
 * 
 * @author pjh
 * @date 2023-05-02
 */
@Service
public class CustomerLinkmanServiceImpl implements ICustomerLinkmanService 
{
    @Resource
    private CustomerLinkmanMapper customerLinkmanMapper;

    /**
     * 查询客户联系人
     * 
     * @param id 客户联系人主键
     * @return 客户联系人
     */
    @Override
    public CustomerLinkman selectCustomerLinkmanById(Long id)
    {
        return customerLinkmanMapper.selectCustomerLinkmanById(id);
    }

    /**
     * 查询客户联系人列表
     * 
     * @param customerLinkman 客户联系人
     * @return 客户联系人
     */
    @Override
    public List<CustomerLinkman> selectCustomerLinkmanList(CustomerLinkman customerLinkman)
    {
        return customerLinkmanMapper.selectCustomerLinkmanList(customerLinkman);
    }

    /**
     * 新增客户联系人
     * 
     * @param customerLinkman 客户联系人
     * @return 结果
     */
    @Override
    public int insertCustomerLinkman(CustomerLinkman customerLinkman)
    {
        Assert.notNull(customerLinkman.getCustomerId(),"所属客户不能为空");
        Assert.notNull(customerLinkman.getLinkman(),"联系人名字不能为空");
        Assert.notNull(customerLinkman.getPhone(),"电话号码不能为空");
        Assert.notNull(customerLinkman.getGender(),"性别不能为空");
        // 手机号码校验
        String customerPhone = customerLinkman.getPhone();
        Assert.state(PhoneUtils.isPhoneNumber(customerPhone),"手机号码格式错误");

        // 设置默认任职状态
        customerLinkman.setPositionState(BusinessConstants.CUSTOMER_ON_JOB);

        // 设置录入人(当前登录的员工)
        customerLinkman.setInputUser(SecurityUtils.getUserId());
        // 设置录入时间
        customerLinkman.setInputTime(DateUtils.getNowDate());
        return customerLinkmanMapper.insertCustomerLinkman(customerLinkman);
    }

    /**
     * 修改客户联系人
     * 
     * @param customerLinkman 客户联系人
     * @return 结果
     */
    @Override
    public int updateCustomerLinkman(CustomerLinkman customerLinkman)
    {
        Assert.notNull(customerLinkman.getCustomerId(),"所属客户不能为空");
        Assert.notNull(customerLinkman.getLinkman(),"联系人名字不能为空");
        Assert.notNull(customerLinkman.getPhone(),"电话号码不能为空");
        Assert.notNull(customerLinkman.getGender(),"性别不能为空");
        Assert.notNull(customerLinkman.getPositionState(),"任职状态不能为空");
        // 手机号码校验
        String customerPhone = customerLinkman.getPhone();
        Assert.state(PhoneUtils.isPhoneNumber(customerPhone),"手机号码格式错误");

        return customerLinkmanMapper.updateCustomerLinkman(customerLinkman);
    }

    /**
     * 批量删除客户联系人
     * 
     * @param ids 需要删除的客户联系人主键
     * @return 结果
     */
    @Override
    public int deleteCustomerLinkmanByIds(Long[] ids)
    {
        return customerLinkmanMapper.deleteCustomerLinkmanByIds(ids);
    }

    /**
     * 删除客户联系人信息
     * 
     * @param id 客户联系人主键
     * @return 结果
     */
    @Override
    public int deleteCustomerLinkmanById(Long id)
    {
        return customerLinkmanMapper.deleteCustomerLinkmanById(id);
    }

    @Override
    public List<CustomerLinkman> selectCustomerLinkmanDTOList(CustomerLinkmanDTO customerLinkmanDTO) {
        return customerLinkmanMapper.selectCustomerLinkmanDTOList(customerLinkmanDTO);
    }

    @Override
    public List<CustomerLinkman> selectCustomerLinkmanListNoPage() {
        return customerLinkmanMapper.selectCustomerLinkmanListNoPage();
    }


}
